<template>
	<div style="padding-bottom: 100px;">
		<view style="background-color: #ffffff;margin: 10px;border-radius: 12px;padding: 15px;">
			<uni-forms ref="form" :modelValue="formData" :rules="rules" labelWidth="90px">
				<uni-forms-item label="货物名称" name="goodsName" required>
					<uni-easyinput type="text" v-model="formData.goodsName" placeholder="请输入货物名称" />
				</uni-forms-item>
				<uni-forms-item label="生产企业" name="produceCompany">
					<uni-easyinput type="text" v-model="formData.produceCompany" placeholder="请输入生产企业" />
				</uni-forms-item>
				<uni-forms-item label="生产地" name="produceDistrictId">
					<uni-data-picker placeholder="请选择地址" popup-title="请选择所在地区" :localdata="produceDistrictId"
						style="background-color: white;" v-model="formData.produceDistrictId">
					</uni-data-picker>
					<view style="margin-top: 20px;">
						<uni-easyinput  type="text"
							v-model="formData.produceAddress" placeholder="请输入详细地址" />
					</view>
				</uni-forms-item>
				<uni-forms-item label="货物类型" name="goodsType">
					<uni-data-select v-model="formData.goodsType" :localdata="goodsType"
						style="background-color: white;" />
				</uni-forms-item>
				<uni-forms-item label="包装" name="packingType">
					<uni-data-select v-model="formData.packingType" :localdata="packingType"
						style="background-color: white;" />
				</uni-forms-item>
				<uni-forms-item label="货号" name="goodCode">
					<uni-easyinput type="text" v-model="formData.goodCode" placeholder="请输入货号" />
				</uni-forms-item>
				<uni-forms-item label="货物上市时间" name="marketTime">
					<uni-datetime-picker type="date" v-model="formData.marketTime" />
				</uni-forms-item>
				<uni-forms-item label="物品描述" name="goodsDesc">
					<uni-easyinput type="textarea" v-model="formData.goodsDesc" placeholder="请输入物品描述" />
				</uni-forms-item>
				<!-- <view style="display: flex;">
					<view style="flex: 1;">
						<uni-forms-item label="货物数量(件)" name="num">
							<uni-easyinput type="number" v-model="formData.num" placeholder="货物数量" />
						</uni-forms-item>
						<uni-forms-item label="货物体积(立方)" name="bulk">
							<uni-easyinput type="number" v-model="formData.bulk" placeholder="货物体积" />
						</uni-forms-item>
					</view>
					<view style="flex: 1;">
						<uni-forms-item label="重量(吨)" name="weight" labelAlign="right">
							<uni-easyinput type="number" v-model="formData.weight" placeholder="货物重量" />
						</uni-forms-item>
						<uni-forms-item label="包装" labelAlign="center">
							<uni-data-select v-model="formData.sourcePackage" :localdata="sourcePackage"
								style="background-color: white;" />
						</uni-forms-item>
					</view>
				</view> -->
				<!-- <uni-forms-item label="货物附件">
					<uni-file-picker limit="3" file-mediatype="all"
						title="*点击可上传货物附件，支持png/jpg/jpeg/gif/pdf/doc/docx/xls/xlsx等格式,文件大小不超过10M,数量不超过3个*"></uni-file-picker>
				</uni-forms-item> -->
				<uni-forms-item label="备注" name="remark">
					<uni-easyinput type="textarea" v-model="formData.remark" placeholder="货物相关备注说明" />
				</uni-forms-item>
			</uni-forms>
		</view>
		<view
			style="background-color: #ffffff;z-index: 2;height: 70px;width:100%;position: fixed;bottom: 0;display: flex;box-shadow: 0px 0px 11px rgba(0, 0, 0, 0.1);">
			<view
				style="display: flex;align-items: center;width: 100%;height: 60px;justify-content: center;">
				<view v-if="goodId==''" @click="submit()" hover-class="buttonHover"
					style="height: 35px;background-color: #bf5f09;color: #ffffff;width: 100px;display: flex;justify-content: center;align-items: center;border-radius: 4px;">
					确定新增</view>
				<view v-else @click="submit_update()" hover-class="buttonHover"
					style="height: 35px;background-color: #bf5f09;color: #ffffff;width: 100px;display: flex;justify-content: center;align-items: center;border-radius: 4px;">
					保存</view>
			</view>
		</view>
	</div>
</template>

<script>
	import {
		getSourceDetails,
		addSource,
		updateSourceProfile
	} from '@/api/system/source.js'
	import {getDicts,getAllAddressTree} from '@/api/system/dict.js'
	
	export default {
		data() {
			return {
				formData: {
					goodsName:'',
					produceEnterprise:'',
					produceDistrictId:'',
					produceAddress: '',
					goodsType: '',
					packingType: '',
					goodCode:'',
					marketTime:'',
					goodsDesc:'',
					remark: '',
				},
				// 校验规则
				rules: {
					goodsName: {
						rules: [{
								required: true,
								errorMessage: '请输入货物名称',
							}
						]
					},
					// produceCompany: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请输入生产企业',
					// 	}]
					// },
					// produceDistrictId: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请选择生产地址',
					// 	}]
					// },
					// goodsType: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请选择货物类型',
					// 	}]
					// },
					// packingType: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请选择包装',
					// 	}]
					// },
					// goodCode: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请输入货号',
					// 	}]
					// },
					// marketTime: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请选择货物上市日期',
					// 	}]
					// }
				},
				//地区下拉框
				produceDistrictId: [],
				//下拉框 ->货物类型
				goodsType: [],
				//下拉框 ->包装类型
				packingType: [],
				imageStyles: {
					width: '100%',
					height: 200,
					border: {
						radius: '4px'
					}
					
				},
				goodId:'',
			}
		},
		onLoad(e) {
			if (e.goodId) {
				//设置顶部导航栏标题
				uni.setNavigationBarTitle({
					title: '编辑货源'
				})
			
				uni.showLoading({
					title: '正在加载',
				})
				this.goodId = e.goodId
				//获取货源详情
				getSourceDetails(e.goodId).then(res => {
					uni.hideLoading()
					if (res.code == '200') {
						this.formData = res.data		
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			}
		},
		created() {
			//获取货物类型下拉框
			getDicts('goods_type').then(res=>{
				this.goodsType=res.data.map(item=>{
					return {
						value:item.dictValue,
						text:item.dictLabel
					}
				})
			})
			
			//获取包装类型下拉框
			getDicts('packing_type').then(res=>{
				this.packingType=res.data.map(item=>{
					return {
						value:item.dictValue,
						text:item.dictLabel
					}
				})
			})
			
			//获取全国地址信息->地区下拉框
			getAllAddressTree().then(res=>{
				this.produceDistrictId=res.data
			})
		},
		methods: {
			//提交货源
			submit() {
				let that = this
				this.$refs.form.validate().then(res => {
					uni.showLoading({
						title: '正在提交...',
					})
					addSource(that.formData).then(res => {
						uni.hideLoading()
						if (res.code == '200') {
							uni.showToast({
								title: '提交成功',
								icon: 'none'
							})
							let pages = getCurrentPages(); // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
							let nowPage = pages[pages.length - 1]; //当前页页面实例
							let prevPage = pages[pages.length - 2]; //上一页页面实例
							prevPage.$vm.otherFun('0') // 給上一頁綁定方法otherFun,傳參0（tabIndex：0）
							uni.navigateBack({
								delta: 1 // 可以不傳delta值，默認為1
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
						}
					})
				}).catch(err => {
					uni.showToast({
						title: err[0].errorMessage,
						icon: 'none'
					})
				})
			},
			//修改货源
			submit_update() {
				let that = this
				this.$refs.form.validate().then(res => {

					uni.showLoading({
						title: '正在提交...',
					})
					that.formData.goodId = that.goodId
					updateSourceProfile(that.formData).then(res => {
						uni.hideLoading()
						if (res.code == '200') {
							uni.showToast({
								title: '修改成功',
								icon: 'none'
							})
							let pages = getCurrentPages(); // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
							let nowPage = pages[pages.length - 1]; //当前页页面实例
							let prevPage = pages[pages.length - 2]; //上一页页面实例
							prevPage.$vm.otherFun('0') // 給上一頁綁定方法otherFun,傳參0（tabIndex：0）
							setTimeout(() => {
								uni.navigateBack({
									delta: 1 // 可以不傳delta值，默認為1
								})
							}, 1000)
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
						}
					})
			
				}).catch(err => {
					uni.showToast({
						title: err[0].errorMessage,
						icon: 'none'
					})
				})
			},
		},
	}
</script>

<style scoped>
.buttonHover {
		opacity: 0.6;
	}
	::v-deep .file-title[data-v-363ace0e] {
	    font-size: 12px;
	    color: #333;
	}
</style>